package tech.aistar.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import tech.aistar.model.entity.Cart;
import tech.aistar.model.entity.Product;
import tech.aistar.model.entity.User;
import tech.aistar.model.vo.Result;
import tech.aistar.service.ICartService;
import tech.aistar.service.IProductService;
import tech.aistar.service.impl.CartServiceImpl;
import tech.aistar.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/4/23 3:10 下午
 */
@WebServlet(urlPatterns = "/login/carts/*")
public class CartController extends HttpServlet {
    private IProductService productService = new ProductServiceImpl();
    private ICartService cartService = new CartServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       String pathInfo = req.getPathInfo();
       switch (pathInfo) {
           case "/add":
               //1. 获取Pid
               resp.setContentType("application/json;charset=utf-8");
               PrintWriter out = resp.getWriter();

               String pid = req.getParameter("pid");
               if (pid != null) {
                   Product p = productService.getById(Integer.valueOf(pid));

                   Cart cart = new Cart();
                   cart.setProductId(p.getId());
                   cart.setProductName(p.getName());
                   cart.setCounts(1);
                   cart.setProductImg(p.getProductImg());
                   cart.setProductRemark(p.getRemark());
                   cart.setProductPrice(p.getPrice());
                   cart.setPrice(cart.getCounts() * p.getPrice());

                   User user = (User) req.getSession().getAttribute("user");
                   cart.setUserId(user.getId());
                   cartService.addCart(cart);

                   Result result = new Result("200", "添加成功", null);

                   out.print(JSON.toJSONString(result));
               }
               break;
           case "/index":
               req.getRequestDispatcher("/WEB-INF/cart/index.jsp").forward(req, resp);
               break;
           case "/content":

               User user = (User) req.getSession().getAttribute("user");
               String pageNow = req.getParameter("pageNow");
               if (pageNow == null) {
                   pageNow = "1";
               }
               PageInfo<Cart> pageInfo = cartService.findAll(user.getId(), Integer.valueOf(pageNow), 3);
               req.setAttribute("pageInfo", pageInfo);
               req.getRequestDispatcher("/WEB-INF/cart/content.jsp").forward(req, resp);
               break;
           case "/del":
               //1. 接受cart购物车id
               resp.setContentType("application/json;charset=utf-8");
               PrintWriter out2 = resp.getWriter();
               //调用service  delById(id)

               String id = req.getParameter("id");
               int count = 0;
               if (null != id && id.trim().length() > 0) {
                   count = cartService.delById(Integer.valueOf(id));

               }
               Result result;

               if (count != 0) {
                   result = new Result("200", "删除成功", null);
               } else {

                   result = new Result("500", "删除失败", null);

               }

               out2.print(JSON.toJSONString(result));
               break;

           //

           case "/update":
               //1. 接受cart购物车id
               resp.setContentType("application/json;charset=utf-8");
               PrintWriter out3 = resp.getWriter();
               //调用service  delById(id)

                  String cardId = req.getParameter("cardId");

                  String pnum = req.getParameter("p_name");

                  int  x=cartService.updatez(Integer.valueOf(cardId),Integer.valueOf(pnum));

                  Result result1 ;

                  if(x!=0)
                  {
                      result1= new Result("200","更新成功",null);


                  }
                  else
                  {

                      result1 = new Result("500","更新失败",null);

                  }

                  out3.print(JSON.toJSONString(result1));

                   break;

       }
    }
}
